﻿using SIPSDk.CallArgs;
using SIPSDk.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SIPSDk.Pjsip
{
    public abstract class Telephony : IDisposable
    {
        public Telephony()
        {
        }

        #region Errors

        private string lastErrorText = string.Empty;

        /// <summary>
        /// Error on last command called.
        /// </summary>
        public string LastErrorText
        {
            get
            {
                return lastErrorText;
            }
            protected set
            {
                lastErrorText = value;
            }
        }
        
        #endregion Errors

        #region Dispose

        public virtual void Dispose()
        {
        }

        #endregion

        public abstract TelephonyState State
        {
            get;
        }

        public event EventHandler StateChanged;

        protected void CallStateChanged()
        {
            StateChanged?.Invoke(this, EventArgs.Empty);
        }

        public abstract void Register(string Login, string Password, string Domain, int Port);

        #region Call operations

        public abstract void MakeCall(string PhoneNumber);

        public abstract void Mute();

        public abstract void Unmute();

        public abstract void Transfer(string Login);

        public abstract void HangUp();

        public abstract void SendDTMFMessage(string msg);

        #endregion Call operations

        #region Incoming call

        public event EventHandler<ComingCallArgs> IncomingCall;
        public event EventHandler<ComingCallArgs> IncomingCallEnded;

        protected void CallIncomingCall(string PhoneNumber)
        {
            IncomingCall?.Invoke(this, new ComingCallArgs(PhoneNumber));
        }
        
        protected void CallIncomingCallEnded(string PhoneNumber)
        {
            IncomingCallEnded?.Invoke(this, new ComingCallArgs(PhoneNumber));
        }

        public abstract void AcceptIncomingCall();

        public abstract void DeclineIncomingCall();

        #endregion Incoming call

        public event EventHandler<DTMFMessageReceiveArgs> DTMFMessageReceived;
        protected void OnDTMFMessageReceived(object sender, DTMFMessageReceiveArgs e)
        {
            DTMFMessageReceived?.Invoke(sender, e);
        }

        public abstract bool CheckDevice();
    }
}
